const koa = require('koa')
const Router = require('koa-router')
const cors = require("koa-cors");
const bodyparser = require("koa-bodyparser");
const router = new Router()
const app = new koa()
const RPCClient = require('@alicloud/pop-core').RPCClient;
let OSS = require('ali-oss');
let client = new OSS({
  region: "oss-cn-shanghai",
  accessKeyId: "LTAIePTupMCUb6ti",
  accessKeySecret: "jZq2Nhqjoji0yNmNub7HptfePyRvtB",
  bucket: "tb-photo"
});
router.post('/', async (ctx, next) => {
  let Title = ctx.request.body.Title ? ctx.request.body.Title : 'anyone'
  let FileName = ctx.request.body.FileName ? ctx.request.body.FileName : 'anyone'
  function initVodClient(accessKeyId, secretAccessKey, ) {
    var regionId = 'cn-shanghai';   // �㲥�����������
    var client = new RPCClient({
      accessKeyId: accessKeyId,
      secretAccessKey: secretAccessKey,
      endpoint: 'http://vod.' + regionId + '.aliyuncs.com',
      apiVersion: '2017-03-21'
    });
    return client;
  }
  // var client = initVodClient('LTAIbL1pouyXk1xg', 'M6ZuD56FKdlpRjCfsqNluMEXT4jJsR');
  var client = initVodClient('LTAIePTupMCUb6ti', 'jZq2Nhqjoji0yNmNub7HptfePyRvtB');
  let result = await client.request("CreateUploadVideo", {
    Title,
    FileName
  }, {}).then(function (response) {
    return response
  }).catch(function (response) {
    return response
    console.log('ErrorCode = ' + response.data.Code);
    console.log('ErrorMessage = ' + response.data.Message);
    console.log('RequestId = ' + response.data.RequestId);
  });
  ctx.body = {
    code: 200,
    data: result
  }
})
router.post('/oss', async (ctx, next) => {
  console.log(1)
  return false
  client.put('object', 'localfile').then(function (r1) {
    console.log('put success: %j', r1);
    return client.get('object');
  }).then(function (r2) {
    console.log('get success: %j', r2);
  }).catch(function (err) {
    console.error('error: %j', err);
  });
})
app.use(cors({
  origin: function (ctx) {
    if (ctx.url === '/test') {
      return false;
    }
    return '*';
  },
  exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
  maxAge: 5,
  credentials: true,
  allowMethods: ['GET', 'POST', 'DELETE'],
  allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}))
  .use(bodyparser())
  .use(router.routes())
  .use(router.allowedMethods());
app.listen(3999)